<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<fmt:setBundle basename="messages"/>
<script type="text/javascript" src="js/calendar/PopupWindow.js"></script>
<script type="text/javascript" src="js/calendar/CalendarPopup.js"></script>
<script type="text/javascript" src="js/calendar/AnchorPosition.js"></script>
<script type="text/javascript" src="js/calendar/date.js"></script>
<script type="text/javascript">
		var calFechaNacimiento = new CalendarPopup("div_fechaNacimiento");
	    calFechaNacimiento.showNavigationDropdowns();
	   	calFechaNacimiento.setCssPrefix("TEST");
	    calFechaNacimiento.setYearSelectStartOffset(80);
   		function openCalendar(dateField){
			calFechaNacimiento.select(dateField,'img_purchasedate','dd/MM/yyyy'); 
			return false;
   		}
   		
   		function validateForm(){
   			var remarks      = document.getElementById("remarks");
   			var purchasedate = document.getElementById("purchasedate");
   			var status       = document.getElementById("status"); 
   		  	var errors = "";   			
			
   			if(trim(purchasedate.value) == ''){
   				errors = errors + '<bean:message key="error.message.futurepurchase.wrong.empy"/>\n';
		    }else if(!isValidDate(trim(purchasedate.value))){
		    	errors = errors + '<bean:message key="error.message.futurepurchase.wrong.date"/>\n';
		    }
   			if(status.value == -1){
   				errors = errors + '<bean:message key="error.select.status"/>\n';   	   		
   			}
   			if( errors == "" ){
   	            return true;
   	        }else{
   	            alert( '<bean:message key="label.common.errors"/> : \n' + errors );
   				return false;
   	        }
   		}	
</script>

<DIV ID="div_fechaNacimiento" STYLE="position:absolute;visibility:hidden;background-color:white;"></DIV>
<html:form action="/saveOrUpdateInstructionPurchaseHeader" onsubmit="return validateForm();">
	<table border="0" cellspacing="1"  bgcolor="#CCCCC0" width="500">
		<tr align="left" class="titulocolumna">
			<td class="titulocolumna" align="center" colspan="4"><bean:message key="label.purchases.instructions"/> </td>
		</tr>
		<tr>
			<td class="etiqueta"><html:hidden property="view.id"/><bean:message key="label.purchases.instructions.reference"/>:</td>
			<td class="etiqueta_campo"> <bean:write name="InstructionPurchaseForm" property="view.reference" /> </td>
			<td class="etiqueta"><bean:message key="label.common.comments"/>:</td>
			<td rowspan="3" valign="top"   class="etiqueta_campo" width="55%">
				<c:choose>
					<c:when test="${InstructionPurchaseForm.view.purchaseStatus == INSTRUCTION_PURCHASE_STATUS_OPENED_ID}">
						<html:textarea onfocus="this.select();" styleId="remarks" property="view.comments" rows="3" cols="40" styleClass="campo_texto" />
					</c:when>
					<c:otherwise>
						<bean:write name="InstructionPurchaseForm" property="view.comments"/>
					</c:otherwise>
				</c:choose>
			</td>
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="message.futurepurchase.purchase.date.single"/></td>
			<td class="etiqueta_campo">
				<c:choose>
					<c:when test="${InstructionPurchaseForm.view.purchaseStatus == INSTRUCTION_PURCHASE_STATUS_OPENED_ID}">
						<html:text property="view.date" styleClass="campo_texto" styleId="purchasedate" onfocus="this.select();" style="width:70px;" size="10px;" />			 	
						<a href="javascript:var obj_dummy = openCalendar(document.getElementById('purchasedate'));" class="pointerhand" NAME="img_purchasedate" ID="img_purchasedate"><img id="fechatrigger" src="images/calendar.gif" border="0" /></a>
					</c:when>
					<c:otherwise>
						<bean:write name="InstructionPurchaseForm" property="view.date"/>
					</c:otherwise>
				</c:choose>
			 </td>
			<td  class="etiqueta_campo"></td>		
		</tr>
		<tr>
			<td class="etiqueta"><bean:message key="label.purchaseinstructiondetail.status"/>: </td>
			<td   class="etiqueta_campo">
				<c:choose>
					<c:when test="${InstructionPurchaseForm.view.purchaseStatus == INSTRUCTION_PURCHASE_STATUS_OPENED_ID}">
						<html:select styleId="status" property="view.purchaseStatus" styleClass="campo_texto">
							<html:option value="-1"><bean:message key="label.purchases.instructions.select"/></html:option>
							<html:option value="1"><bean:message key="label.purchaseinstructiondetail.status.1"/></html:option>
							<html:option value="2"><bean:message key="label.purchaseinstructiondetail.status.2"/></html:option>
							<html:option value="3"><bean:message key="label.purchaseinstructiondetail.status.3"/></html:option>									
						</html:select> 
					</c:when>
					<c:otherwise>
						<fmt:message key="label.purchaseinstructiondetail.status.${InstructionPurchaseForm.view.purchaseStatus}"></fmt:message>
					</c:otherwise>
				</c:choose>
			</td>
			<td  class="etiqueta_campo"></td>			
		</tr>
	</table>
	<br/>
	<table width="100%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
		<tr align="left" class="titulocolumna">
			<td class="titulocolumna" align="center" colspan="13"><bean:message key="label.purchaseinstructiondetail.list"/> </td>
		</tr>
		<tr>
			<td class="titulocolumna" align="center" width="6%">
				<c:if test="${InstructionPurchaseForm.view.purchaseStatus != INSTRUCTION_PURCHASE_STATUS_CANCELED_ID}">
					<a href="#>" onclick="location.href = 'purchaseInstructionDetailsClearNew.do?purchaseInstructionId=<c:out value="${purchaseInstructionId}" />';">
						<img alt="<bean:message key="common.add"/> <bean:message key="label.purchaseinstructiondetail.list"/>" src="images/nuevo.gif" border="0">
					</a>	
				</c:if>
			</td>
			<td class="titulocolumna" align="center" width="2%">
				<fmt:message key="label.purchaseinstructiondetail.line"/>
			</td>	
			<td class="titulocolumna" align="center" width="23%">
				<fmt:message key="label.purchaseinstructiondetail.customer"/>
			</td>	
			<td class="titulocolumna" align="center" width="23%">
				<fmt:message key="label.purchaseinstructiondetail.product"/>
			</td>	
			<td class="titulocolumna" align="center" width="5%">
				<fmt:message key="label.purchaseinstructiondetail.quantity"/>
			</td>	
			<td class="titulocolumna" align="center" width="6%">
				<fmt:message key="label.purchaseinstructiondetail.cornquantity"/>
			</td>
			<td class="titulocolumna" align="center" colspan="2">
				<fmt:message key="label.purchaseinstructiondetail.adquired.futures"/>
			</td>
			<td class="titulocolumna" align="center" width="5%">
				<fmt:message key="label.purchaseinstructiondetail.type"/>
			</td>
			<td class="titulocolumna" align="center" width="7%">
				<fmt:message key="label.purchaseinstructiondetail.purchase.strategy"/>
			</td>
			<td class="titulocolumna" align="center" width="10%">
				<fmt:message key="label.purchaseinstructiondetail.type.strategy"/>
			</td>
			<td class="titulocolumna" align="center" width="6%">
				<fmt:message key="label.purchaseinstructiondetail.status"/>
			</td>
		</tr>
		<c:forEach items="${PURCHASE_INSTRUCTION_DETAILS}" var="detail" varStatus="loop">
			<tr class='<c:choose><c:when test="${loop.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>'>
				<td valign="top">
					<a href="#>" onclick="location.href = 'purchaseInstructionDetailsEditFromDetail.do?pid=<c:out value="${detail.purchaseInstructionDetailid}" />';"><img alt="<bean:message key="common.edit"/> <c:out value="${detail.row}" />" src="images/edit_customer_item.png" border="0" width="12" height="12"></a>
					<a href="#>" onclick="location.href = 'goToPurchaseDetailReceptionFromDetail.do?purchaseInstructionDetailId=<c:out value="${detail.purchaseInstructionDetailid}" />'"><img alt="<bean:message key="label.purchases.instructions.reception"/> <c:out value="${detail.row}" />" width="12" height="12" src="images/hand.png" border="0"></a>
					<a href="#>" onclick="location.href='purchaseInstructionDetailAttachmentFromDetail.do?pid=<c:out value="${detail.purchaseInstructionDetailid}" />';" class="ligas"><img alt="<bean:message key="label.purchases.instructions.attachment"/>  <c:out value="${detail.row}" />" src="images/attach.png" border="0" width="12" height="12"></a>
				</td>
				<td valign="top" align="center">			
					<c:out value="${detail.row}" />
				</td>
				<td align="left" valign="top">
					<c:out value="${detail.customerView.customerId}" /> - <c:out value="${detail.customerView.shortDescription}" />
				</td>
				<td align="left">
					<table width="99%" align="center" cellpadding="2" cellspacing="1" border="0" bgcolor="#CCCCC0">
						<c:forEach items="${detail.productList}" var="productos" varStatus="loopInside">
							<tr class='<c:choose><c:when test="${loopInside.index%2 == 0}"><c:out value="columnadatoPar" /></c:when><c:otherwise><c:out value="columnadatoNon" /></c:otherwise></c:choose>'>
								<td align="center">
									<c:out value="${productos.productId}"></c:out>
								</td>
								<td align="left">
									<c:out value="${productos.shortDescription}"></c:out>
								</td>
							</tr>
						</c:forEach>
					</table>
				</td>
				<td align="right" valign="top">
					<c:out value="${detail.itemQuantity}" />
				</td>			
				<td align="right" valign="top">
					<c:out value="${detail.cornQuantity}" />
				</td>			
				<td  width="3%" align="right" valign="top">
					<c:out value="${detail.futures}" />
				</td>
				<td  width="4%" align="right" valign="top">
					<c:out value="${detail.tons}" />
				</td>
				<td valign="top">
					<fmt:message key="label.futuretype.${detail.futureTypeId}"/>
				</td>
				<td valign="top">
					<fmt:message key="label.strategyid.${detail.instructionStrategyId}"/>
				</td>
				<td valign="top">
					<fmt:message key="label.strategytypeid.${detail.instructionTypeId}"/>
				</td>
				<td valign="top">
					<fmt:message key="label.purchaseinstructiondetail.status.${detail.status}"/>
				</td>
			</tr>
		</c:forEach>
	</table>
	<br/>
	<table border="0" cellspacing="1"  bgcolor="#CCCCC0" width="500">
		<tr>
			<td colspan="4" align="center" class="columnadatoPar">
				<c:if test="${InstructionPurchaseForm.view.purchaseStatus == INSTRUCTION_PURCHASE_STATUS_OPENED_ID}">
					<html:submit styleClass="boton_large"  property="sendEmail" ><bean:message key="label.common.save.and.sendMail"/></html:submit>
					<html:submit styleClass="boton"><bean:message key="label.common.save"/></html:submit>
				</c:if>
				<html:button styleClass="boton" property="backButton" onclick="location.href = 'instructionPurchaseCreateList.do'"><bean:message key="label.common.cancel"/></html:button> 
			</td>
		</tr>
	</table>
</html:form>